package dao

import (
	"context"

	"go-mall/common/util"
	"go-mall/dal/model"
	"go-mall/logic/do"
)

type DemoDao struct {
	ctx context.Context
}

func NewDemoDao(ctx context.Context) *DemoDao {
	return &DemoDao{ctx: ctx}
}

func (dd *DemoDao) GetAllDemos() (demos []*model.DemoOrder, err error) {
	if err := DB().WithContext(dd.ctx).Find(&demos).Error; err != nil {
		return nil, err
	}
	return demos, nil
}

func (dd *DemoDao) CreateDemoOrder(
	demoOrder *do.DemoOrder) (*model.DemoOrder, error) {

	model := new(model.DemoOrder)
	if err := util.CopyProperties(model, demoOrder); err != nil {
		return nil, err
	}


	if err := DB().
		WithContext(dd.ctx).
		Create(model).
		Error; err != nil {
		return nil, err
	}
	return model, nil
}
